<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <ul class="list">
        <li></li>
    </ul>
    <button>获取电影</button>
    <button>添加电影</button>
    <button>修改电影</button>
    <button>局部修改PATCH大写</button>
    <button>删除电影</button>
    <script>
        // 获取ul和按钮
        let ul = document.querySelector('ul');
        let btns = document.querySelectorAll('button');

        btns[0].onclick = async function () {
            let res = await fetch('http://api.zhibao.com:3000/movies').then(val => val.json())
            //    console.log(res);

            let str = ``;
            res.forEach(item => {
                str += `<li>${item.name}</li>`;
            });

            ul.innerHTML = str;
        }
    
        btns[1].onclick =async function(){
           let res = await fetch('http://api.zhibao.com:3000/movies',{
                method:"post",
                headers:{
                    // "content-type":"application/x-www-form-urlencoded"
                    "content-type":"application/json"
                },
                // body:`name=电影&director=爱敲代码&star=小女孩`
                body:JSON.stringify({
                    name:"肖申克的救赎",
                    director:"戴维斯",
                    star:"摩根"
                })
            })

            console.log(res);
        }
    
        btns[2].onclick =async function(){
            // 全部修改 - 不设置，就清空
           let res = await fetch('http://api.zhibao.com:3000/movies/7',{
                method:"put",
                headers:{
                    "content-type":'application/x-www-form-urlencoded'
                },
                body:`director=弗兰克·德拉邦特`
            })

            console.log(res);
        }
    
        btns[3].onclick =async function(){
          let res =await  fetch('http://api.zhibao.com:3000/movies/7',{
                method:"PATCH",
                headers:{
                    'content-type':"application/json"
                },
                body:JSON.stringify({
                    star:"摩根·弗里曼"
                })
            })

            console.log(res);
        }
    
        btns[4].onclick =async function(){
           let res = await fetch('http://api.zhibao.com:3000/movies/7',{
                method:"delete",
            })
            
            console.log(res);
        }
    </script>
</body>

</html>